<?php
/**
 * @author		João Batista Neto
 * @package		rpo
 * @subpackage	http\interfaces
 * @category	Http, Client
 * @license		http://opensource.org/licenses/gpl-license.php GNU Public License
 */
namespace rpo\http\interfaces;

use \LogicException;
use rpo\base\Object;
use rpo\http\interfaces\IHttpConnection;
use rpo\http\interfaces\IHttpState;

/**
 * Define a interface para um estado HTTP
 * @author		João Batista Neto
 * @abstract
 * @package		rpo
 * @subpackage	http\interfaces
 * @category	Http, Client
 * @license		http://opensource.org/licenses/gpl-license.php GNU Public License
 */
abstract class AbstractHttpState extends Object implements IHttpState {
	/**
	 * @var AbstractHttpState
	 */
	private static $state;

	/**
	 * Constroi o objeto de estado
	 */
	protected function __construct(){
		parent::__construct();
	}

	/**
	 * Muda o estado da conexão
	 * @param IHttpConnection $connection
	 */
	public function changeState( IHttpConnection $connection ){
		$connection->changeState( $this );
	}

	/**
	 * Fecha a conexão
	 * @param IHttpConnection $connection
	 */
	public function close( IHttpConnection $connection ){
		throw new LogicException( sprintf( '%s não implementa o método %s' , get_class( $this ) , __METHOD__ ) );
	}

	/**
	 * Recupera uma instância do objeto de estado
	 * @final
	 * @return AbstractHttpState
	 */
	final public static function instance(){
		if ( !self::$state ){
			$class = get_called_class();
			self::$state = new $class();
		}

		return self::$state;
	}

	/**
	 * Carrega dados
	 * @param IHttpConnection $connection
	 */
	public function load( IHttpConnection $connection ){
		throw new LogicException( sprintf( '%s não implementa o método %s' , get_class( $this ) , __METHOD__ ) );
	}

	/**
	 * Abre a conexão
	 * @param IHttpConnection $connection
	 */
	public function open( IHttpConnection $connection ){
		throw new LogicException( sprintf( '%s não implementa o método %s' , get_class( $this ) , __METHOD__ ) );
	}

	/**
	 * Envia dados
	 * @param IHttpConnection $connection
	 */
	public function send( IHttpConnection $connection ){
		throw new LogicException( sprintf( '%s não implementa o método %s' , get_class( $this ) , __METHOD__ ) );
	}
}